package com.example.result.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.result.SemanticResultHandler;
import com.iflytek.aiui.assist.player.AIUIPlayer;
import com.iflytek.aiui.assist.player.AIUIPlayer.ContentType;
import com.iflytek.aiui.assist.player.AIUIPlayer.PlayItem;
import com.iflytek.aiui.assist.player.AIUIPlayerKitVer;
import com.iflytek.aiui.servicekit.tts.SpeechSynthesizer;
import android.content.Context;
import android.text.TextUtils;

/**
 * 语义结果抽象类。
 * 
 * @author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * @date 2016年8月18日 上午10:40:00 
 *
 */
public abstract class SemanticResult {
	// 演示的service类型
	public static enum ServiceType {
		WEATHER, 					// 天气
		TRAIN, 						// 火车
		FLIGHT, 					// 航班
		MUSICX, 					// 音乐播放&控制
		NUMBER_MASTER, 			// 数字纠错
		CHAT, 						// 闲聊
		POETRY,						// 对诗
		SMARTHOME,					// 智能家居
		OTHER
	}

	static HashMap<String, ServiceType> serviceMap = new HashMap<String, ServiceType>();

	static {
		serviceMap.put("weather", ServiceType.WEATHER);
		serviceMap.put("train", ServiceType.TRAIN);
		serviceMap.put("flight", ServiceType.FLIGHT);
		serviceMap.put("musicX", ServiceType.MUSICX);
		serviceMap.put("numberMaster", ServiceType.NUMBER_MASTER);
		serviceMap.put("chat", ServiceType.CHAT);
		serviceMap.put("poetry", ServiceType.POETRY);
		serviceMap.put("smartHome", ServiceType.SMARTHOME);
	}

	public final static String KEY_TEXT = "text";
	
	public final static String KEY_ANSWER = "answer";
	
	// 数字朗读方式
	public final static String KEY_RDN = "rdn";
	
	// 数字朗读方式，当成数值朗读，123将朗读成“一百二十三”
	public final static int RDN_VALUE = 1;
	
	// 数字朗读方式，当成字符朗读，123将朗读成“一二三”
	public final static int RDN_STRING = 2;
	
	protected String service;
	
	protected String answerText = "";
	
	protected JSONObject json = null;

	public static ServiceType getServiceType(String service) {
		ServiceType type = serviceMap.get(service);
		if (null == type) {
			type = ServiceType.OTHER;
		}
		
		if (-1 != service.lastIndexOf("smartHome")) {
			return ServiceType.SMARTHOME;
		}
		
		return type;
	}

	public SemanticResult(String service, JSONObject json) {
		this.service = service;
		this.json = json;
	}

	public void handleResult(Context context) {
		if (null != json) {
			if (json.has(KEY_ANSWER)) {
				// 合成answer中的text字段
				try {
					JSONObject answer = json.getJSONObject(KEY_ANSWER);
					String text = answer.getString(KEY_TEXT);
					
					if (!TextUtils.isEmpty(text)) {
						int readNumber = RDN_VALUE;
						final ServiceType serviceType = getServiceType();

						if (serviceType == ServiceType.NUMBER_MASTER
								|| serviceType == ServiceType.TRAIN
								|| serviceType == ServiceType.FLIGHT) {
							// 数字纠错、火车、航班业务中的数字当字符读
							readNumber = RDN_STRING;
						} 
						
						SpeechSynthesizer tts = SpeechSynthesizer.getSynthesizer();
						if (null != tts) {
							tts.setParameter(KEY_RDN, readNumber + "");
						}
						
						AIUIPlayerKitVer player = SemanticResultHandler.getAIUIPlayer();
						if (null != player) {
							PlayItem item = new PlayItem(ContentType.TEXT, text, null);
							List<PlayItem> itemList = new ArrayList<AIUIPlayer.PlayItem>();
							itemList.add(item);
							
							player.playItems(itemList, null);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}

	protected abstract void doAfterTTS();

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public String getAnswerText() {
		return answerText;
	}

	public ServiceType getServiceType() {
		return getServiceType(service);
	}

	public JSONObject getJson() {
		return json;
	}

	public void setJson(JSONObject json) {
		this.json = json;
	}

}
